<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="操作时间：">
						<div style="display: flex;align-items: center;">
							<a-form-item style="flex: 1;margin-right:12px;" name="startTimeCreated">
								<a-date-picker placeholder="请选择开始日期" class="block" format="YYYY-MM-DD"
											   v-model:value="searchFormState.startTimeCreated" allow-clear/>
							</a-form-item>
							<a-form-item style="flex: 1" name="endTimeCreated">
								<a-date-picker placeholder="请选择结束日期" class="block" format="YYYY-MM-DD"
											   v-model:value="searchFormState.endTimeCreated" allow-clear/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="操作单号：" name="orderNo">
						<a-input v-model:value="searchFormState.orderNo" placeholder="请输入操作单号" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-space>
						<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
						<a-button @click="() => formRef.resetFields()">重置</a-button>
					</a-space>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="form.onOpen()">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
<!--					<a-button danger @click="removeBatch">-->
<!--						删除-->
<!--					</a-button>-->
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'buyer'">
					<div>客户名称：{{ record.buyerName }}</div>
					<div>联系电话：{{ record.buyerPhone }}</div>
				</template>
				<template v-if="column.dataIndex === 'store'">
					{{ $TOOL.tableData('ORG_CATEGORY', record.storeCode, 'code') }}
				</template>
				<template v-if="column.dataIndex === 'payInfo'">
					<div>支付方式：{{ $TOOL.dictTypeData('PAY_TYPE', record.payType) }}</div>
					<div>支付金额：{{ record.payAmount }}</div>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<template v-if="record.status === 0">
							<a-button size="small" type="link" @click="form.onOpen(record)">编辑</a-button>
							<a-divider type="vertical"/>
						</template>
						<a-button size="small" type="link" @click="form.onOpen(record, true)">详情</a-button>
						<a-divider type="vertical"/>
						<a-popconfirm title="确定作废售卡单吗？" @confirm="handleCancel(record)">
							<a-button :disabled="record.canceled === 1"  size="small" type="link">作废</a-button>
						</a-popconfirm>
<!--						<a-dropdown :trigger="['click']">-->
<!--							<a-button size="small" type="link" @click.prevent>-->
<!--								更多-->
<!--								<DownOutlined/>-->
<!--							</a-button>-->
<!--							<template #overlay>-->
<!--								<a-menu>-->
<!--									<a-menu-item>-->
<!--										<a-popconfirm :disabled="record.status === 1" title="确定审批吗？"-->
<!--													  @confirm="audit(record)">-->
<!--											<a-button size="small" type="link" :disabled="record.status === 1">审核-->
<!--											</a-button>-->
<!--										</a-popconfirm>-->
<!--									</a-menu-item>-->
<!--									<a-menu-item>-->
<!--										<a-popconfirm :disabled="record.status === 1" title="确定要删除吗？"-->
<!--													  @confirm="remove(record)">-->
<!--											<a-button :disabled="record.status === 1" size="small" type="link" danger>-->
<!--												删除-->
<!--											</a-button>-->
<!--										</a-popconfirm>-->
<!--									</a-menu-item>-->
<!--								</a-menu>-->
<!--							</template>-->
<!--						</a-dropdown>-->
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="form" @successful="table.refresh(true)"/>
</template>

<script setup name="cardSale">
	import {message} from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import tool from '@/utils/tool'
	import Form from './form.vue'

	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	const toolConfig = {refresh: true, height: true, columnSetting: false, striped: false}
	const columns = [
		{
			title: '操作单号',
			dataIndex: 'orderNo',
			width: 200
		},
		{
			title: '操作门店',
			dataIndex: 'store',
			width: 180
		},
		{
			title: '操作时间',
			dataIndex: 'createdTime',
			sorter: true,
			width: 220,
			align: 'center'
		},
		{
			title: '联系人',
			dataIndex: 'buyer'
		},
		{
			title: '支付信息',
			dataIndex: 'payInfo'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 240
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let cardTypeOptions = tool
		.tableList('CRM_CARD_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	// 列表数据
	const loadData = (parameter) => {
		const params = Object.assign(parameter, searchFormState)
		if (params.startTimeCreated) {
			params.startTimeCreated = params.startTimeCreated.format('YYYY-MM-DD')
		}
		if (params.endTimeCreated) {
			params.endTimeCreated = params.endTimeCreated.add(1,'day').format('YYYY-MM-DD')
		}
		return crmApi.cardSaleOrderPage(params).then((res) => {
			return res
		})
	}

	const handleCancel = (record) => {
		let params = {
			id: record.id
		}
		crmApi.cardSaleOrderCancel(params).then(() => {
			table.value.refresh(true)
			message.success('审核成功')
		})
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.removeCardType(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.removeCardType(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>
.ant-card-head-title {
	text-align: center !important;
}
</style>
